/*
Main input: 	
	> tmp/raw_stimmbez_full.dta [prepped struc indicators, precinct-lvl] 
	> newdata/estimation_prep_ltw18_voter.dta [prepped voter panel, precinct-lvl] 
	> tmp/stadtbez_postalcomp_partyoutc.dta [prepped postal party outcomes, district-lvl] 
	> rawdata/RWI/02_data/final/election_rwi_red_sb_final.dta [quoted rents, precinct-lvl] 

Output: newdata/estimation_prep_ltw18_stadtbez.dta 
			[District level panel, ID: stadtbez x wahl_id]
			
Main task:
	> aggregate precinct-level controls to DISTRICT-level
	> aggregate precinct-level treatment variables to DISTRICT-level
	> Merge district-level party outcomes 

Note: only used in the estimation file: 03h

*/


* PULL: structural indicators (precinct-level)
use "$tmp/raw_stimmbez_full.dta", clear

	* drop precinct and turnout
	drop sb gebietsartschluessel stimmbezirksart wahlber* waehl* kow_sti* anz* unguel*

	* collapse to district level
	gcollapse (sum) ew_* wb_* hh_* (mean) avg_dur, by(wahl stadtbez) labelformat(#sourcelabel#) fast

* Convert structural indicators back to shares
	foreach v of varlist ew_dt ew_ausl* ew_mihi ew_ledig ew_married hh_sgl hh_kids{
		replace `v' = `v' /ew_ges
		assert inrange(`v',0,1) if wahl != "BTW2009"
	}

* save
	tempfile struktur_ind_stadtbez 
	save `struktur_ind_stadtbez'


* PULL: prepped address-level panel
use "$newdata/estimation_prep_ltw18_voter.dta", clear	

frame copy default tmp, replace
frame tmp {
		// collapse from address to precinct level
		gcollapse (mean) waehler_gesamt waehler_mit_wahlschein ungueltige_stimmen ///
			wahlber_gesamt wahlber_ohne_wahlschein wahlber_mit_wahlschein wahlber_nicht_im_waehlerv, ///
			by(wahl wahl_type wahl_id jahr stadtbez sb_new) labelformat(#sourcelabel#) fast

		// collapse from precinct to district level (sum over precinct)
		gcollapse (sum) waehler_gesamt waehler_mit_wahlschein ungueltige_stimmen ///
			wahlber_gesamt wahlber_ohne_wahlschein wahlber_mit_wahlschein wahlber_nicht_im_waehlerv, ///
			by(wahl wahl_type wahl_id jahr stadtbez) labelformat(#sourcelabel#) fast

* save
	tempfile votes_stadtbez 
	save `votes_stadtbez'
}
	
		
	* MERGE avg. quoted rents (precinct level)
		merge m:1 jahr sb_new using "$rawdata/RWI/02_data/final/election_rwi_red_sb_final", assert(3) keepusing(mpreis_flats_rent) nogen 
	
	* Collapse treatment variables (reassignments, distance) to district level
	gcollapse (mean) *street_dist* *treat* mpreis_*, ///
			by(wahl wahl_type wahl_id jahr stadtbez) labelformat(#sourcelabel#) fast

	* MERGE back aggr. votes
	merge 1:1 wahl wahl_type wahl_id jahr stadtbez using `votes_stadtbez', assert(3) nogen
	
	* MERGE with district level outcomes (STADTbezirk)
	merge m:1 stadtbez wahl using  "$tmp/stadtbez_postalcomp_partyoutc.dta", assert(3) nogen
	format sbez_shr* %9.4f

	* MERGE covariates
	merge 1:1 wahl stadtbez using `struktur_ind_stadtbez', assert(3) nogen
		
		
* gen logs
	assert ew_ges > 0
	gen 	ln_ew_ges = ln(ew_ges)
	lab var ln_ew_ges 	"Log number of residents"
	
	gen 	ew_ausl = ew_ausl_eu + ew_ausl_else
	lab var ew_ausl		"\% foreign residents"
	gen 	ew_dtmihi = ew_mihi - ew_ausl
	lab var ew_dtmihi 	"\% non-native German residents"
	gen 	ew_biodt = ew_dt - ew_dtmihi			
	lab var ew_biodt	"\% native German residents"
	gen 	wb_anteil = wahlber_gesamt/ew_ges	
	lab var wb_anteil 	"\% inhabitants eligible to vote"


* Rescale Covariates to Percentages
	foreach v of varlist wb_* ew_* hh_* {
		 if inrange(`v',0,1) {
			replace `v' = 100*`v'
		 }
	}
	
 * (Re-)label Variables
	lab var ew_ges 		"Number of residents"
	lab var ln_ew_ges 	"Log number of residents"
	lab var ew_biodt	"\% native German residents"
	lab var ew_mihi 	"\% migrant residents (MiHi+foreigners)"
	lab var ew_dtmihi 	"\% non-native German residents"
	lab var ew_ausl		"\% foreign residents"
	lab var ew_ausl_eu	"\% EU foreigners"
	lab var ew_ausl_else "\% non-EU foreigners"
	lab var ew_dt		"\% German residents"
	lab var ew_ledig	"\% single residents"
	lab var ew_married 	"\% married residents"
	
	lab var wahlber_gesamt "Number of eligible voters"
	lab var wb_anteil 	"\% inhabitants eligible to vote"
	lab var wb_dt 		"\% Germans in the electorate"
	lab var wb_ausl		"\% EU-foreigners in the electorate"
	lab var wb_18t24 	"\% electorate aged 18-24"
	lab var wb_25t34 	"\% electorate aged 25-34"
	lab var wb_35t44 	"\% electorate aged 35-44"
	lab var wb_45t59 	"\% electorate aged 45-59"
	lab var wb_60plus 	"\% electorate aged 60+"
	
	lab var avg_dur		"Average duration of residence"
	lab var hh_kids		"\% households with children"
	lab var hh_sgl 		"\% single-person households"
	
	lab var mpreis_flats_rent 	"Average quoted rent per sqm"
	
* save District level panel
	compress
	isid stadtbez wahl_id
	save "$newdata/estimation_prep_ltw18_stadtbez.dta", replace
	
	
	
	
